package org.sproject.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.sexternaljar.picturerandomizer.Randomizer;

public class Run {

	/**
	 * @param args
	 * @throws InterruptedException
	 * @throws CloneNotSupportedException
	 */
	static String baseUrl = "http://questionpro.com/t/AIejEZNM9r";

	// private static Selenium selenium;
	public static void main(String[] args) throws InterruptedException,
			CloneNotSupportedException {
		// TODO Auto-generated method stub
		
		// selenium = new WebDriverBackedSelenium(driver, baseUrl);
		FillTheForm();
	}

	private static void FillTheForm()
			throws InterruptedException {
		// Go to the web
		// selenium.open(baseUrl);
		WebDriver driver = new FirefoxDriver();
		//Selenium seleniumDriver = new WebDriverBackedSelenium(driver, baseUrl);
		driver.get(baseUrl);
		Thread.sleep(5000);

		// Auto fill question 1
		String[] q1ChoiceXpath = { "//*[@id=\"128625697ID\"]",
				"//*[@id=\"128625698ID\"]", "//*[@id=\"128625700ID\"]",
				"//*[@id=\"130607736ID\"]", "//*[@id=\"130607737ID\"]",
				"//*[@id=\"130607738ID\"]", "//*[@id=\"130607739ID\"]",
				"//*[@id=\"130607740ID\"]", "//*[@id=\"130607741ID\"]" };

		Randomizer rdForQ1 = new Randomizer("c:/Test/test.jpg");
		String q1 = rdForQ1.randomElement(q1ChoiceXpath);
		do {
			//seleniumDriver.mouseOver(q1);
			driver.findElement(By.xpath(q1)).click();
//			driver.findElement(By.className("SurveyRunBody")).sendKeys(Keys.PAGE_DOWN);
//			driver.findElement(By.className("SurveyRunBody")).sendKeys(Keys.PAGE_UP);
		} while (!driver.findElement(By.xpath(q1)).isSelected());
		
		
		//Thread.sleep(3000);

		// Auto fill question 2
		String[] q2ChoiceXpath = { "//*[@id=\"130608019ID\"]",
				"//*[@id=\"130608020ID\"]", "//*[@id=\"130608021ID\"]",
				"//*[@id=\"130608022ID\"]" };
		Randomizer rdForQ2 = new Randomizer("c:/Test/test1.jpg");
		String q2 = rdForQ2.randomElement(q2ChoiceXpath);
		do {
			//seleniumDriver.mouseOver(q2);
			driver.findElement(By.xpath(q2)).click();
//			driver.findElement(By.className("SurveyRunBody")).sendKeys(Keys.PAGE_DOWN);
//			driver.findElement(By.className("SurveyRunBody")).sendKeys(Keys.PAGE_UP);
		} while (!driver.findElement(By.xpath(q2)).isSelected());
		//Thread.sleep(3000);
		
		// Auto fill question 3
		String[] q3ChoiceXpath = { "//*[@id=\"130608157ID\"]",
				"//*[@id=\"130608158ID\"]", "//*[@id=\"130608159ID\"]",
				"//*[@id=\"130608160ID\"]", "//*[@id=\"130608161ID\"]",
				"//*[@id=\"130608162ID\"]" };
		Randomizer rdForQ3 = new Randomizer("c:/Test/test2.jpg");
		String q3 = rdForQ3.randomElement(q3ChoiceXpath);
		do {
			//seleniumDriver.mouseOver(q3);
			driver.findElement(By.xpath(q3)).click();
//			driver.findElement(By.className("SurveyRunBody")).sendKeys(Keys.PAGE_DOWN);
//			driver.findElement(By.className("SurveyRunBody")).sendKeys(Keys.PAGE_UP);
		} while (!driver.findElement(By.xpath(q3)).isSelected());
		// Handling the "other" option, fill the text
		if (q3 == "//*[@id=\"130608162ID\"]") {
			Randomizer rdForQ3Text = new Randomizer("c:/Test/test3.jpg");
			String[] q3Text = { "grey", "orange", "purple", "brown" };
			driver.findElement(
					By.xpath("/html/body/table/tbody/tr/td/table/tbody/tr/td/div/form/table/tbody/tr/td/table/tbody/tr[6]/td/div/table/tbody/tr[3]/td/table/tbody/tr[6]/td/span/input"))
					.clear();
			driver.findElement(
					By.xpath("/html/body/table/tbody/tr/td/table/tbody/tr/td/div/form/table/tbody/tr/td/table/tbody/tr[6]/td/div/table/tbody/tr[3]/td/table/tbody/tr[6]/td/span/input"))
					.sendKeys(rdForQ3Text.randomElement(q3Text));

		}
		//Thread.sleep(3000);
		

		// Auto fill question 4
		String[] q4Text = { "no", "more xxx", "null" };
		Randomizer rdForQ4Text = new Randomizer("c:/Test/test3.jpg");
		driver.findElement(
				By.xpath("/html/body/table/tbody/tr/td/table/tbody/tr/td/div/form/table/tbody/tr/td/table/tbody/tr[8]/td/div/table/tbody/tr/td/table/tbody/tr[3]/td/table/tbody/tr/td[2]/textarea"))
				.sendKeys(rdForQ4Text.randomElement(q4Text));
		//Thread.sleep(3000);
		
		
		
		// Auto submit
		driver.findElement(By.xpath("//*[@id=\"SurveySubmitButtonElement\"]"))
				.click();
		Thread.sleep(5000);
		driver.quit();
		FillTheForm();
	}

}
